package com.example.content.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.RuntimeException
import java.lang.reflect.ParameterizedType

/**
 * Created by zhudongao
 * Desc
 * Date 2020/9/13
 */
abstract class BaseActivity<T : ViewDataBinding, WM : ViewModel> : BaseUIActivity() {
    lateinit var dataBinding: T
    lateinit var viewModel: WM


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding = DataBindingUtil.setContentView(this, getLayoutId())
        dataBinding.lifecycleOwner = this
        viewModel = ViewModelProvider(this).get(getViewModelClass())
        initData()
        initView()
        initListener()
    }

    private fun getViewModelClass(): Class<WM> {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            return type.actualTypeArguments[1] as Class<WM>
        } else {
            throw RuntimeException("not get The property View Module!")
        }
    }

    open fun initListener() {

    }

    open fun initData() {

    }

    open fun initView() {

    }

    abstract fun getLayoutId(): Int

}